package com.zlict.commonelasticsearch.controller;

import com.zlict.commonelasticsearch.service.ElasticsearchDiskMonitorService;
import com.zlict.commonelasticsearch.service.ElasticsearchILMService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Elasticsearch磁盘监控控制器
 * 
 * @author zlict
 */
@RestController
@RequestMapping("/elasticsearch/disk")
public class ElasticsearchDiskMonitorController {
    
    @Autowired
    private ElasticsearchDiskMonitorService diskMonitorService;
    
    @Autowired
    private ElasticsearchILMService elasticsearchILMService;
    
    /**
     * 手动检查磁盘空间并触发ILM策略调整
     * 
     * @param thresholdMB 阈值（MB）
     * @return 操作结果
     */
    @PostMapping("/check")
    public String checkDiskSpace(@RequestParam(defaultValue = "1024") long thresholdMB) {
        try {
            boolean emergencyTriggered = diskMonitorService.checkDiskSpaceAndTriggerILM(thresholdMB);
            
            if (emergencyTriggered) {
                return "磁盘空间不足，已触发紧急ILM策略";
            } else {
                return "磁盘空间充足，无需调整ILM策略";
            }
        } catch (Exception e) {
            return "磁盘空间检查失败: " + e.getMessage();
        }
    }
    
    /**
     * 基于ES集群状态的智能磁盘检查
     * 
     * @param thresholdMB 阈值（MB）
     * @return 操作结果
     */
    @PostMapping("/check-with-es")
    public String checkDiskSpaceWithES(@RequestParam(defaultValue = "1024") long thresholdMB) {
        try {
            boolean emergencyTriggered = diskMonitorService.checkDiskSpaceWithESStatus(thresholdMB);
            
            if (emergencyTriggered) {
                return "磁盘空间不足，已触发紧急ILM策略";
            } else {
                return "磁盘空间充足，无需调整ILM策略";
            }
        } catch (Exception e) {
            return "磁盘空间检查失败: " + e.getMessage();
        }
    }
    
    /**
     * 手动恢复正常的ILM策略
     * 
     * @param retentionMonths 保留月数
     * @return 操作结果
     */
    @PostMapping("/restore")
    public String restoreNormalILMStrategy(@RequestParam(defaultValue = "6") int retentionMonths) {
        try {
            boolean restored = diskMonitorService.restoreNormalILMStrategy(retentionMonths);
            
            if (restored) {
                return "正常ILM策略恢复成功";
            } else {
                return "正常ILM策略恢复失败";
            }
        } catch (Exception e) {
            return "恢复正常ILM策略失败: " + e.getMessage();
        }
    }
    
    /**
     * 获取磁盘空间信息
     * 
     * @param thresholdMB 阈值（MB）
     * @return 磁盘空间信息
     */
    @GetMapping("/info")
    public ElasticsearchDiskMonitorService.DiskSpaceMonitorInfo getDiskSpaceInfo(
            @RequestParam(defaultValue = "1024") long thresholdMB) {
        return diskMonitorService.getDiskSpaceInfo(thresholdMB);
    }
    
    /**
     * 获取集群磁盘使用情况
     * 
     * @return 集群磁盘使用情况
     */
    @GetMapping("/cluster-usage")
    public String getClusterDiskUsage() {
        try {
            String usage = elasticsearchILMService.getClusterDiskUsage();
            return usage;
        } catch (Exception e) {
            return "获取集群磁盘使用情况失败: " + e.getMessage();
        }
    }
    
    /**
     * 获取所有ILM策略
     * 
     * @return 策略信息
     */
    @GetMapping("/policies")
    public String getAllILMPolicies() {
        try {
            String policies = elasticsearchILMService.getAllILMPolicies();
            return policies;
        } catch (Exception e) {
            return "获取ILM策略失败: " + e.getMessage();
        }
    }
    
    /**
     * 创建紧急ILM策略
     * 
     * @return 操作结果
     */
    @PostMapping("/emergency-policy")
    public String createEmergencyPolicy() {
        try {
            boolean success = elasticsearchILMService.createEmergencyILMPolicy();
            
            if (success) {
                elasticsearchILMService.applyEmergencyPolicyToAuditIndices();
                return "紧急ILM策略创建并应用成功";
            } else {
                return "紧急ILM策略创建失败";
            }
        } catch (Exception e) {
            return "创建紧急ILM策略失败: " + e.getMessage();
        }
    }
}



